package com.cookie; /**
 * @author wzm
 * @create 2023-03-13 11:23
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/cookie04")
public class CookieServlet04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = "张三";
        //针对中文报错
        username = URLEncoder.encode(username, "UTF-8");

        Cookie cookie = new Cookie("username",username);
        response.addCookie(cookie);
        response.getWriter().write("ok");


    }
}
